package com.hlhj.carview.aty

import android.content.Intent
import android.net.Uri
import android.support.v7.widget.LinearLayoutManager
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Constant
import com.hlhj.carview.Iview.NothingView
import com.hlhj.carview.R
import com.hlhj.carview.adapter.BCarListAdp
import com.hlhj.carview.bean.BorrowCarListBean
import com.hlhj.carview.bean.ItemAskListBean
import com.hlhj.carview.customView.IteamAskDialog
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.NothingPresenter
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.aty_borrowcar.*
import kotlinx.android.synthetic.main.lo_pubhead.*

/**
 *NeverMore
 *2020/9/11
 **/
class BCarAty:BaseAty<NothingView,NothingPresenter>(), NothingView {
    private val datas=ArrayList<BorrowCarListBean.Data>()
    private lateinit var adp:BCarListAdp

    override fun initView() {
        tvTittle.text="借车"
        adp= BCarListAdp(datas)
        listView.adapter=adp
        listView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

    }

    override fun logicStart() {
        val httpParams = HttpParams()
        httpParams.put("uriPath","boCar")
        OkGo.post<BorrowCarListBean>(Urls.PubUrl)
                .headers("type",3.toString())
                .params(httpParams)
                .converter(object : MyJsonConvert<BorrowCarListBean>(){})
                .adapt(ObservableResponse<BorrowCarListBean>())
                .subscribeOn(Schedulers.io())
                .subscribe(object : CustomAction<Response<BorrowCarListBean>>(this){
                    override fun httpSuccess(t: Response<BorrowCarListBean>) {
                        t.body().data?.let { it1 -> datas.addAll(it1) }
                        adp.notifyDataSetChanged()
                    }
                })
    }

    override fun setListener() {
        adp.setOnItemClickListener { adapter, view, position ->
            val iteamAskDialog = IteamAskDialog(this)
            iteamAskDialog.callback=object :()->Unit{
                override fun invoke() {
                    // 打电话
                    // Intent : 意图.我想去做一件事
                    val t = Intent()
                    // Action:动作.我具体想做什么事
                    // Intent.ACTION_DIAL: 激活拨号界面
                    // Intent.ACTION_CALL: 直接拨打电话
                    t.action = Intent.ACTION_CALL
                    // Data: 数据.具体的动作所需要的附加数据
                    //封装一个拨打电话的intent，并且将电话号码包装成一个Uri对象传入
                    t.data = Uri.parse("tel:" + datas[position].phone)
                    // 通知系统你去帮我干活吧
                    startActivity(t)
                }
            }
          iteamAskDialog.show("打电话给师傅借车？","")

        }
    }

    override fun getContentID(): Int {
        return R.layout.aty_borrowcar
    }

    override fun bindView(): NothingView {
        return this
    }

    override fun createPresenter(): NothingPresenter {
        return NothingPresenter()
    }
}